package com.culturalCenter.dataCenter.service.impl.site;

import com.culturalCenter.dataCenter.entity.site.SiteAlerts;
import com.culturalCenter.dataCenter.mapper.site.SiteAlertsMapper;
import com.culturalCenter.dataCenter.mapper.site.SiteMapper;
import com.culturalCenter.dataCenter.service.site.SiteAlertsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/****
 * @Author:
 * @Description:SiteAlerts业务层接口实现类
 * @Date
 *****/
@Service
public class SiteAlertsServiceImpl implements SiteAlertsService {

    @Resource
    private SiteAlertsMapper siteAlertsMapper;
    @Resource
    private SiteMapper siteMapper;

    @Override
    public PageInfo<SiteAlerts> findPage(int page, int size) {
        return PageHelper.startPage(page, size).doSelectPageInfo(() -> siteAlertsMapper.findPageAll());
    }

    @Override
    public Integer delete(String id) {
        return siteAlertsMapper.deleteById(id);
    }

    @Override
    public Integer update(SiteAlerts siteAlerts) {
        return siteAlertsMapper.update(siteAlerts);
    }

    @Override
    public Integer add(SiteAlerts siteAlerts) {
        return siteAlertsMapper.insert(siteAlerts);
    }

    @Override
    public SiteAlerts findById(String id) {
        return siteAlertsMapper.queryById(id);
    }

    @Override
    public List<SiteAlerts> findAll(SiteAlerts siteAlerts) {
        List<SiteAlerts> siteAlertsList = new ArrayList<>();
        List<?> list = siteAlertsMapper.queryAll(siteAlerts);
        list.forEach(element -> siteAlertsList.add((SiteAlerts) element));
        return siteAlertsList;
    }

    @Override
    public PageInfo<SiteAlerts> getMonitoringRecordList(Date date, int type, String siteCode, int pageNum, int pageSize) {
//        String siteId = siteMapper.getSiteIdBySiteCode(siteCode);
        PageHelper.startPage(pageNum, pageSize);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM");
        String currentMonth = formatter.format(new Date());
        String inputDate = null;
        if (date != null) {
            inputDate = formatter.format(date);
        }
        PageInfo<SiteAlerts> alertsPageInfo = new PageInfo<>(siteAlertsMapper.getMonitoringRecordList(type, (date == null) ? currentMonth : inputDate, siteCode));
        PageHelper.clearPage();
        return alertsPageInfo;
    }

}
